// Ofri Keidar 302933981 group 05

package view.useBeans;

/**
 * Bean class holding order's data
 */
public class OrderData {

	// order's details
	String orderingCustomerNumber;
	String orderedProductName;
	String amount;
	
	// setters
	
	/**
	 * Sets number of customer who made the order
	 * @param orderingCustomerNumber- customer number to set
	 */
	public void setOrderingCustomerNumber(String orderingCustomerNumber) {
		this.orderingCustomerNumber = orderingCustomerNumber;
	}
	
	/**
	 * Sets ordered product's name
	 * @param orderedProductName- product name to set
	 */
	public void setOrderedProductName(String orderedProductName) {
		this.orderedProductName = orderedProductName;
	}
	
	/**
	 * Sets amount of ordered items
	 * @param amount- amount to set
	 */
	public void setAmount(String amount) { this.amount = amount; }

	// getters

	/**
	 * Returns number of customer who made the order
	 * @return ordering customer's number attribute
	 */
	public String getOrderingCustomerNumber() { return orderingCustomerNumber; }

	/**
	 * Returns name of product being ordered
	 * @return ordered product's name attribute
	 */
	public String getOrderedProductName() { return orderedProductName; }

	/**
	 * Returns amount of ordered items
	 * @return amount attribute
	 */
	public String getAmount() { return amount; }
	
	// utilities
	
	/**
	 * Checks if data had been already set.
	 * Data is set if members are not null nor empty strings
	 * @return True if data is set, False if not
	 */
	public boolean isDataSet() {
		
		// validate ordering customer number
		boolean isCustomerNumSet = true;
		if (orderingCustomerNumber == null) { isCustomerNumSet = false; }
		else if (orderingCustomerNumber.equals("")) { isCustomerNumSet = false; }
		
		// validate ordered product name
		boolean isProductNameSet = true;
		if (orderedProductName == null) { isProductNameSet = false; }
		else if (orderedProductName.equals("")) { isProductNameSet = false; }
		
		// validate amount
		boolean isAmountSet = true;
		if (amount == null) { isAmountSet = false; }
		else if (amount.equals("")) { isAmountSet = false; }
		
		// check if parameters are set
		return isCustomerNumSet && isProductNameSet && isAmountSet;
	}
	
	/**
	 * Returns true if numeric data received is valid
	 * (which means, strings hold numbers)
	 * @return True if numeric attributes are valid, False if not
	 */
	public boolean isNumericDataValid() {
		
		// parse numeric values for matching attributes
		Integer customerNumberVal = getCustomerNumberValue();
		Integer amountVal = getAmountValue();
		
		// check if all are valid
		return (customerNumberVal != null) && (amountVal != null); 
	}
	
	/**
	 * Returns customer number's numeric value.
	 * If data is not set or number string does 
	 * not contain a numeric value, returns null
	 * @return customer number's numeric value
	 */
	public Integer getCustomerNumberValue() {
		
		// check if received a customer number
		if (orderingCustomerNumber == null) { return null; }
		if (orderingCustomerNumber.equals("")) { return null; }
		
		// try parsing customer number
		try {
			return Integer.parseInt(orderingCustomerNumber);
		} catch (NumberFormatException e) {
			
			// content is not numeric
			return null;
		}
	}
	
	/**
	 * Returns amount's numeric value.
	 * If data is not set or amount string cannot 
	 * be parsed to a positive integer, returns null
	 * @return amount's numeric value
	 */
	public Integer getAmountValue() {
		
		// check if received amount
		if (amount == null) { return null; }
		if (amount.equals("")) { return null; }
		
		// try parsing amount
		try {
			
			// parse numeric value
			Integer parsedVal = Integer.parseInt(amount);
			
			// check success
			if (parsedVal == null) { return null; }
			
			// parsed value is a number, check if positive
			if (parsedVal <= 0) {
				
				// customer number cannot be positive
				return null; 
			}			
			
			// parsed value is a positive integer
			return parsedVal;

		} catch (NumberFormatException e) {
			
			// content is not numeric
			return null;
		}
	}
}